package cn.bestick.util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author Bestick
 *
 *         be stick...
 *
 *         2015年8月24日 下午5:19:04
 */
public class DbUtil {

	private static DbUtil dbUtil = new DbUtil();

	private DbUtil() {
	}

	public static DbUtil getInstance() {
		return dbUtil;
	}

	/**
	 * 获取数据库连接
	 * 
	 * @return con
	 * @throws Exception
	 */
	public Connection getCon() throws Exception {
		Class.forName(PropertiesUtil.getValue("jdbcName"));
		Connection con = DriverManager.getConnection(
				PropertiesUtil.getValue("dbUrl"),
				PropertiesUtil.getValue("dbUserName"),
				PropertiesUtil.getValue("dbPassword"));
		return con;
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @param con
	 * @throws Exception
	 */
	public void closeCon(Connection con) throws Exception {
		if (con != null) {
			con.close();
		}
	}

	/**
	 * 测试连接
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(PropertiesUtil.getValue("dbUrl"));
		System.out.println(PropertiesUtil.getValue("dbUserName"));
		System.out.println(PropertiesUtil.getValue("dbPassword"));
		try {
			new DbUtil().getCon();
			System.out.println("success");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error");
		}
	}
}
